/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.common.utils;

import cn.simastudio.talos.common.utils.codec.HmacSHA256Utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Usage:
 *
 * @author Charkey
 * @date 2015/5/19 9:36
 */
public class HttpGet {

    private HttpURLConnection httpURLConn;

    public static void main(String[] args) {
        HttpGet hg = new HttpGet();
        hg.doPut();
    }

    public void doGet() {
        try {
            String temp;
            URL url = new URL("http://localhost:8349/header/get");
            httpURLConn = (HttpURLConnection) url.openConnection();
            httpURLConn.setDoOutput(true);
            httpURLConn.setRequestMethod("GET");
            httpURLConn.setIfModifiedSince(999999999);
            httpURLConn.setRequestProperty("Referer", "http://localhost:80");
            httpURLConn.setRequestProperty("User-Agent", "test");

            // id：用于区分用户的UUID
            // nonce：时间戳 +“:”+ 随机码（客户端生成）
            // mac：请求签名

            String nonce = "1418752425:dj83hs9s";
            String requestUri = "/header/get?name=charkey";
            String secretKey = "RxHJ20i407uQADLFuxBdVm+U4dPOB7WkRLt2GhVqM98=";
            String uuid = "6c2b89bf-36b3-4db1-87c5-8251e2f6137b";

            String digest = HmacSHA256Utils.digest(secretKey, (nonce + "\n" + requestUri + "\n" + secretKey + "\n" + uuid + "\n"));
            System.out.println("消息摘要：" + digest);

            httpURLConn.setRequestProperty("Authorization",
                    "MAC id=" + uuid + ",nonce=" + nonce + ",mac=" + digest + "");

            httpURLConn.connect();
            InputStream in = httpURLConn.getInputStream();
            BufferedReader bd = new BufferedReader(new InputStreamReader(in));
            while ((temp = bd.readLine()) != null) {
                System.out.println(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpURLConn != null) {
                httpURLConn.disconnect();
            }
        }
    }

    public void doPut() {
        try {
            String temp;
            URL url = new URL("http://localhost:8349/api/users/1");
            httpURLConn = (HttpURLConnection) url.openConnection();
            httpURLConn.setDoOutput(true);
            httpURLConn.setRequestMethod("PUT");
            httpURLConn.setIfModifiedSince(999999999);
            httpURLConn.setRequestProperty("Referer", "http://localhost:80");
            httpURLConn.setRequestProperty("User-Agent", "test");
            httpURLConn.connect();
            InputStream in = httpURLConn.getInputStream();
            BufferedReader bd = new BufferedReader(new InputStreamReader(in));
            while ((temp = bd.readLine()) != null) {
                System.out.println(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpURLConn != null) {
                httpURLConn.disconnect();
            }
        }
    }

}
